package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.domain.Question;
import com.tanhua.domain.Settings;
import com.tanhua.domain.User;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * Settings服务消费者
 */
@Service
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private QuestionApi questionApi;

    /**
     * 读取用户通用设置
     *
     * @param userId
     * @param phone
     * @return
     */
    public SettingsVo readSettings(Long userId, String phone) {
        //创建返回SettingsVo对象
        SettingsVo vo = new SettingsVo();

        //为vo设置phone和id
        vo.setId(userId);
        vo.setPhone(phone);

        //获取对应陌生人问题信息
        Question question = questionApi.getByUserId(userId);
        //判断有无初始文本并设置,question对象为null则说明未设置过问题
        String strangerQuestion = question == null ? "暂无陌生人问题，快来设置吧" : question.getTxt();
        vo.setStrangerQuestion(strangerQuestion);

        //获取对应消息设置，并设置入vo
        Settings settings = settingsApi.getByUserId(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setLikeNotification(settings.getLikeNotification());
        }

        //返回vo对象
        return vo;
    }

    /**
     * 设置陌生人问题 - 保存或更新
     *
     * @param content
     */
    public void saveQuestion(String content) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用服务提供者方法
        questionApi.saveQuestion(content, userId);
    }

    /**
     * 通知设置 - 保存或更新
     *
     * @param settings
     */
    public void saveNotificationsSetting(Settings settings) {
        //调用服务提供者方法
        settingsApi.saveNotificationsSetting(settings);
    }

    /**
     * 黑名单分页
     *
     * @param page
     * @param size
     * @return
     */
    public PageResult blacklist(int page, int size) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //blackListApi使用方法返回IPage对象
        IPage iPage = blackListApi.blackList(userId,page,size);
        //对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pageResult =new PageResult(page,size, iPage.getTotal(), iPage.getRecords());
        //返回pageResult
        return pageResult;
    }


    /**
     * 黑名单移除
     *
     * @param uid
     */
    public void removeBlackList(Long uid) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用服务提供者方法
        blackListApi.removeBlackList(userId,uid);
    }
}
